include FileUtils

SRC_DIR = File.join(File.dirname(__FILE__), 'sources')
DEST_DIR = File.join(File.dirname(__FILE__), 'rpm', 'SOURCES')

def rm_svn(dir)
  cd dir do 
    system "find -name .svn|xargs rm -rf" 
  end
end

def prepare_source(label)
  src_package = File.join(DEST_DIR, "#{label}.zip")
  rm_f src_package
  
  cd SRC_DIR do
    mkdir_p "tmp"
    cp_r label, "tmp"
    rm_svn "tmp"
    cd "tmp" do
      system "zip -r #{src_package} #{label}"
    end
    rm_rf "tmp"
  end  
end

task :default => :package_all

desc 'build source package for ruby and rubygems'
task :prepare_source do
  prepare_source "ruby-1.8.5"
  prepare_source "rubygems-0.9.2"
  
  work_dir = File.join(File.dirname(__FILE__), 'rpm')  
  system %Q(echo "%_topdir #{work_dir}" > $HOME/.rpmmacros)
end

desc 'make RPM for ruby'
task :ruby_package => :prepare_source do
  system "sudo rpmbuild -bb ruby.spec"
end

desc 'make RPM for rubygems'
task :gems_package => :prepare_source do
  system "sudo rpmbuild -bb rubygems.spec"
end

desc 'make RPM for haproxy'
task :haproxy_package do
  cp(File.join(SRC_DIR, 'haproxy-1.3.8.2.tar.gz'), DEST_DIR)
  system "sudo rpmbuild -bb haproxy.spec"
end

desc 'make umbrella RPM for rubyworks-common (takes long time)'
task :rubyworks_common_package => [:ruby_package, :gems_package, :haproxy_package] do 
  system "rpmbuild -bb rubyworks-common.spec"
end

desc 'make RPM for rails-control-center'
task :control_center_package do
  control_center_dir = File.join(File.dirname(__FILE__), 'control-center')
  cd control_center_dir do
    system "rake"
    system "mv rails-control-center*.zip #{DEST_DIR}"
  end
  system "sudo rpmbuild -bb rails-control-center.spec"
end

desc 'build the entire package'
task :rubyworks_package => [:control_center_package] do 
  system "rpmbuild -bb rubyworks.spec"
  rpm_dir = File.join(File.dirname(__FILE__), 'rpm')
  cd rpm_dir do
    system "createrepo RPMS"
  end
end

desc 'build all packages'
task :package_all => [:rubyworks_common_package, :rubyworks_package] do
end


desc 'build package for mephisto'
task :package_mephisto do
  fakeroot = File.join(File.dirname(__FILE__), 'fakeroot')
  rm_rf fakeroot
  dest_dir = File.join(fakeroot, 'usr', 'local', 'lib')
  mkdir_p dest_dir
  cp_r File.join(SRC_DIR, 'mephisto-0.7.2'), dest_dir
  rm_svn fakeroot
  mv File.join(dest_dir, 'mephisto-0.7.2'), File.join(dest_dir, 'mephisto')

  mkdir_p File.join(dest_dir, 'mephisto', 'log')
  mkdir_p File.join(dest_dir, 'mephisto', 'tmp')
  
  cd File.join(dest_dir, 'mephisto', 'public') do
    file_gsub 'dispatch.cgi', '/opt/local/bin/ruby', '/usr/bin/env ruby'
    file_gsub 'dispatch.rb', '/opt/local/bin/ruby', '/usr/bin/env ruby'
  end

  cp File.join(SRC_DIR, 'mephisto-config', 'mephisto_container.config'), File.join(dest_dir, 'mephisto', 'config')
  system "sudo rpmbuild -bb --buildroot=#{fakeroot} mephisto.spec"
  rm_rf fakeroot
end

def file_gsub(filename, pattern, replacement)
  content = File.open(filename){|f| f.read }
  content.gsub! pattern, replacement
  File.open(filename, "w"){|f| f.write content }
end